package com.tentinet.testtapsviewpager;

import android.graphics.Bitmap;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.tentinet.testtapsviewpager.adapter.ContentViewPagerAdapter;
import com.tentinet.testtapsviewpager.fragment.TestFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager view_pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setWidgetListener();
    }

    /**
     * 初始化Views
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/1/25,14:02
     * <h3>UpdateTime</h3> 2016/1/25,14:02
     * <h3>CreateAuthor</h3>
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * 
     */
    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.main_tabLayout);
        view_pager = (ViewPager) findViewById(R.id.main_view_pager);
    }

    /**
     * 初始化数据
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/1/25,14:02
     * <h3>UpdateTime</h3> 2016/1/25,14:02
     * <h3>CreateAuthor</h3>
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * 
     */
    private void initData() {
        ArrayList<String> tabs = new ArrayList<>();
        tabs.add("现金卷");
        tabs.add("折扣卷");
        tabs.add("抵扣卷");
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            TestFragment fragment = new TestFragment();
            Bundle bundle = new Bundle();
            bundle.putString(TestFragment.FRAGMENT_KEY,"标题" + i);
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        ContentViewPagerAdapter contentViewPager = new ContentViewPagerAdapter(getSupportFragmentManager(),tabs,fragments);

        //设置模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        //设置指示条的高度
        tabLayout.setSelectedTabIndicatorHeight(10);

        tabLayout.setTabsFromPagerAdapter(contentViewPager);
        TabLayout.TabLayoutOnPageChangeListener listener = new TabLayout.TabLayoutOnPageChangeListener(tabLayout);
        view_pager.addOnPageChangeListener(listener);

        view_pager.setAdapter(contentViewPager);
        //必须先给ViewPager设置适配器后才能再给ViewPager设置ViewPager 这一步不能缺少
        tabLayout.setupWithViewPager(view_pager);

    }
    /**
     * 设置监听
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/1/25,14:03
     * <h3>UpdateTime</h3> 2016/1/25,14:03
     * <h3>CreateAuthor</h3>
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     *
     */
    private void setWidgetListener() {
        //设置tab监听
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int currentPosition = tab.getPosition();
                view_pager.setCurrentItem(currentPosition);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
